package com.hgd.igreencar.biz.impl;


import com.hgd.igreencar.biz.IActualAirQualityBiz;
import com.hgd.igreencar.dao.ActualAirQualityMapper;
import com.hgd.igreencar.dao.AirElementMapper;
import com.hgd.igreencar.dao.LocationNumberMapper;
import com.hgd.igreencar.entity.ActualAirQuality;
import com.hgd.igreencar.entity.SpeficCityDateData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class ActualAirQualityBizImpl implements IActualAirQualityBiz {
    @Autowired
    private ActualAirQualityMapper actualAirQualityMapper;
    @Autowired
    private LocationNumberMapper locationNumberMapper;
    @Autowired
    private AirElementMapper airElementMapper;

    /**
     * 功能：传入实体，根据cities_number和record_time匹配表actual_air_quality_table里的数据项
     *     若未传入日期，则默认今日
     *     如果没有传入城市，默认所有城市; 如果传入城市，只会传入一个城市
     *     如果没有传入成分，默认所有成分; 如果传入成分，只会传入一种成分
     */

    @Override
    public List<ActualAirQuality> querySpecificActualAirQuality(@RequestBody ActualAirQuality actualAirQuality) {
        //查看传入
        System.out.print(actualAirQuality);
        //必要：处理日期
        if (actualAirQuality.getRecordTime() == null) {
            // 如果没有传入日期，则默认显示今日
            Date now = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String sdate = sdf.format(now);
            Date defaultDate = java.sql.Date.valueOf(sdate);
            actualAirQuality.setRecordTime((java.sql.Date) defaultDate);
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String sdate = sdf.format(actualAirQuality.getRecordTime());
            actualAirQuality.setRecordTime(java.sql.Date.valueOf(sdate));
        }
        //处理城市
        if (actualAirQuality.getLocationName() == "" || actualAirQuality.getLocationName() == null) {
            //没有传入城市
            //检查是否传入成分
            if (actualAirQuality.getElementName() == "" || actualAirQuality.getElementName() == null) {
                System.out.println("没有传入城市,没有传入空气成分");
                List<ActualAirQuality> aaq = actualAirQualityMapper.querySpecificActualAirQuality(actualAirQuality);
                for(ActualAirQuality actualAirQualityl : aaq) {
                    actualAirQualityl.setLocationName(locationNumberMapper.querySpecificCityName(actualAirQualityl.getCitiesNumber()));
                    actualAirQualityl.setElementName(airElementMapper.querySpecificElementName(actualAirQualityl.getElementId()));
                }
                return aaq;
            } else {
                System.out.println("没有传入城市,传入空气成分");
                actualAirQuality.setElementId(airElementMapper.querySpecificElementId(actualAirQuality.getElementName()));
                List<ActualAirQuality> aaq = actualAirQualityMapper.querySpecificActualAirQuality(actualAirQuality);
                for(ActualAirQuality actualAirQualityl : aaq) {
                    actualAirQualityl.setLocationName(locationNumberMapper.querySpecificCityName(actualAirQualityl.getCitiesNumber()));
                    actualAirQualityl.setElementName(airElementMapper.querySpecificElementName(actualAirQualityl.getElementId()));
                }
                return aaq;
            }

        }else{
            //传入城市
            actualAirQuality.setCitiesNumber(locationNumberMapper.querySpecificCityId(actualAirQuality.getLocationName()));
            //检查是否传入成分
            if (actualAirQuality.getElementName() == "" || actualAirQuality.getElementName() == null) {
                System.out.println("传入城市，没有传入空气成分");
                List<ActualAirQuality> aaq = actualAirQualityMapper.querySpecificActualAirQuality(actualAirQuality);
                for(ActualAirQuality actualAirQualityl : aaq) {
                    actualAirQualityl.setLocationName(locationNumberMapper.querySpecificCityName(actualAirQualityl.getCitiesNumber()));
                    actualAirQualityl.setElementName(airElementMapper.querySpecificElementName(actualAirQualityl.getElementId()));
                }
                return aaq;
            } else {
                System.out.println("传入城市，传入空气成分");
                actualAirQuality.setElementId(airElementMapper.querySpecificElementId(actualAirQuality.getElementName()));
                List<ActualAirQuality> aaq = actualAirQualityMapper.querySpecificActualAirQuality(actualAirQuality);
                for(ActualAirQuality actualAirQualityl : aaq) {
                    actualAirQualityl.setLocationName(locationNumberMapper.querySpecificCityName(actualAirQualityl.getCitiesNumber()));
                    actualAirQualityl.setElementName(airElementMapper.querySpecificElementName(actualAirQualityl.getElementId()));
                }
                return aaq;
            }
        }
    }

    @Override
    public ArrayList<SpeficCityDateData> querySpecificCityDateData(ActualAirQuality actualAirQuality) {
        //处理城市
        actualAirQuality.setCitiesNumber(locationNumberMapper.querySpecificCityId(actualAirQuality.getLocationName()));
        //若传入日期则处理
        //且只会返回一条数据
        if (actualAirQuality.getRecordTime() != null){
            System.out.println("传入日期");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String sdate = sdf.format(actualAirQuality.getRecordTime());
            actualAirQuality.setRecordTime(java.sql.Date.valueOf(sdate));
            List<ActualAirQuality> aaq = actualAirQualityMapper.querySpecificActualAirQuality(actualAirQuality);
            for(ActualAirQuality actualAirQualityl : aaq) {
                actualAirQualityl.setLocationName(locationNumberMapper.querySpecificCityName(actualAirQualityl.getCitiesNumber()));
                actualAirQualityl.setElementName(airElementMapper.querySpecificElementName(actualAirQualityl.getElementId()));
            }
            SpeficCityDateData s = new SpeficCityDateData();
            s.locationName = aaq.get(0).getLocationName();
            s.recordTime = aaq.get(0).getRecordTime();

            for(ActualAirQuality actualAirQualityl : aaq) {
                System.out.println(actualAirQualityl);
                if(actualAirQualityl.getElementName().equals("PM2.5")){
                    System.out.println("是PM2.5");
                    s.PM25 = actualAirQualityl.getElementEvaluate();
                    System.out.println(s);
                }else if (actualAirQualityl.getElementName().equals("PM10")){
                    s.PM10 = actualAirQualityl.getElementEvaluate();
                }else if(actualAirQualityl.getElementName().equals("CO")){
                    s.CO = actualAirQualityl.getElementEvaluate();
                }else if(actualAirQualityl.getElementName().equals("SO2")){
                    s.SO2 = actualAirQualityl.getElementEvaluate();
                }else if(actualAirQualityl.getElementName().equals("NO2")){
                    s.NO2 = actualAirQualityl.getElementEvaluate();
                }else if(actualAirQualityl.getElementName().equals("O3")){
                    s.O3 = actualAirQualityl.getElementEvaluate();
                }
            }
            ArrayList<SpeficCityDateData> sl = new ArrayList<>();
            sl.add(s);
            return sl;
        }else{
            //没有传入日期
            System.out.println("没有传入日期");
            List<ActualAirQuality> aaq = actualAirQualityMapper.querySpecificActualAirQuality(actualAirQuality);
            ArrayList<SpeficCityDateData> s2 = new ArrayList<>();
            int i = 0;
            for(ActualAirQuality actualAirQualityl : aaq) {
                //如果还没有处理
                if(!actualAirQualityl.isHasProcess()){
                    //初始化s的城市名和日期
                    System.out.println("还没有处理");
                    actualAirQualityl.setLocationName(locationNumberMapper.querySpecificCityName(actualAirQualityl.getCitiesNumber()));
                    actualAirQualityl.setElementName(airElementMapper.querySpecificElementName(actualAirQualityl.getElementId()));
                    SpeficCityDateData s = new SpeficCityDateData();
                    s.locationName = actualAirQualityl.getLocationName();
                    s.recordTime = actualAirQualityl.getRecordTime();
                    //找到所有是这个日期的
                    //初始化成分
                    for(int j = 0 ; j < aaq.size(); j++){
                        //遍历寻找日期相同项
                        System.out.println(j);
                        if(!aaq.get(i).isHasProcess() && aaq.get(i).getRecordTime() == s.recordTime){
                            System.out.println("找到了日期相同项，如下");
                            System.out.println(aaq.get(i));
                            if(aaq.get(i).getElementName().equals("PM2.5")){
                                System.out.println("是PM2.5");
                                s.PM25 = aaq.get(i).getElementEvaluate();
                                aaq.get(i).setHasProcess(true);
                                System.out.println(s);
                            }else if (aaq.get(i).getElementName().equals("PM10")){
                                aaq.get(i).setHasProcess(true);
                                s.PM10 = aaq.get(i).getElementEvaluate();
                            }else if(aaq.get(i).getElementName().equals("CO")){
                                aaq.get(i).setHasProcess(true);
                                s.CO = aaq.get(i).getElementEvaluate();
                            }else if(aaq.get(i).getElementName().equals("SO2")){
                                aaq.get(i).setHasProcess(true);
                                s.SO2 = aaq.get(i).getElementEvaluate();
                            }else if(aaq.get(i).getElementName().equals("NO2")){
                                aaq.get(i).setHasProcess(true);
                                s.NO2 = aaq.get(i).getElementEvaluate();
                            }else if(aaq.get(i).getElementName().equals("O3")){
                                aaq.get(i).setHasProcess(true);
                                s.O3 = aaq.get(i).getElementEvaluate();
                            }
                        }
                    }
                    s2.add(s);
                    i++;
                    if(i>10){
                        break;
                    }
                }
            }
            return s2;
        }
    }
}
